package com.douwong.student.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.model.PrepareResource;
import com.douwong.student.utils.FileUtils;

import java.util.ArrayList;


public class PrepareLoadingAdapter extends BaseAdapter {

	private Context context ;
	private ArrayList<PrepareResource> list;
	private OnClickListener onClick;
	public PrepareLoadingAdapter(Context context, ArrayList<PrepareResource>list, OnClickListener onClick){
		this.context=context;
		this.list=list;
		this.onClick=onClick;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}
	@SuppressLint("NewApi")
	@Override
	public View getView(int paramInt, View view, ViewGroup parent) {
		Holder holder=null;
		if(view==null){
			holder=new Holder();
			view=LayoutInflater.from(context).inflate(R.layout.cloud_share_item, null);
			holder.file_icon_img=(ImageView) view.findViewById(R.id.file_icon_img);
			holder.file_name_text=(TextView) view.findViewById(R.id.file_name_text);
			holder.upload_user_text=(TextView) view.findViewById(R.id.upload_user_text);
			holder.create_time_text=(TextView) view.findViewById(R.id.create_time_text);
			holder.pro_text=(TextView) view.findViewById(R.id.pro_text);
			holder.load_pro=(ProgressBar) view.findViewById(R.id.load_pro);
			holder.isupload_text=(TextView) view.findViewById(R.id.isupload_text);
			holder.upload_delete_text=(TextView) view.findViewById(R.id.upload_delete_text);
			view.setTag(holder);
		}else {
			holder=(Holder) view.getTag();
		}
		if(list.size()>0){
			holder.file_icon_img.setImageResource(getBackgroundByName(list.get(paramInt).getAttaSuffix()));


			/*------start------*/
			if (list.get(paramInt).getMcTitle()!=null){
				//file_name_text.setText(list.get(paramInt).getMcTitle());
				holder.file_name_text.setText(list.get(paramInt).getMcTitle());
			}else {
				holder.file_name_text.setText(list.get(paramInt).getFileName());
			}

			/*------end------*/
		//	holder.file_name_text.setText(list.get(paramInt).getFileName());
			String pro= FileUtils.formatFileSize(list.get(paramInt).getCurrentPro())+"/"+FileUtils.formatFileSize(list.get(paramInt).getFileSize());
			try {
				holder.load_pro.setProgress((int)(list.get(paramInt).getCurrentPro()*100/list.get(paramInt).getFileSize()));
			} catch (Exception e) {
				e.printStackTrace();
			}
			holder.pro_text.setText(pro);
			holder.isupload_text.setOnClickListener(onClick); 
			holder.isupload_text.setTag(paramInt);
			holder.upload_delete_text.setOnClickListener(onClick);
			holder.upload_delete_text.setTag(paramInt);
//			holder.upload_user_text.setText(list.get(paramInt).getSubjectName()+"\t"+list.get(paramInt).getUserName());
			holder.create_time_text.setText(list.get(paramInt).getCreatetime());
			if(!list.get(paramInt).isPause()){
				holder.isupload_text.setBackground(context.getResources().getDrawable(R.mipmap.class_load_paus));
			}else{
				holder.isupload_text.setBackground(context.getResources().getDrawable(R.mipmap.class_isupload));
			}
			
		}
		return view;
	}
	class Holder {
		ImageView file_icon_img;
		TextView file_size_text,upload_user_text,create_time_text,file_name_text,pro_text,
		isupload_text,upload_delete_text;
		ProgressBar load_pro;
	}
	private int getBackgroundByName(String fileName){
		String str=fileName.toLowerCase();
		if(str.equals("jpg")||str.equals("png")){
			return R.mipmap.file_pic;
		}else if(str.equals("mp4")){
			return R.mipmap.file_video;
		}else if(str.equals("mp3")){
			return R.mipmap.sound;
		}else{
			return R.mipmap.class_share_file;
		}
		
	}

}
